/*
 * Copyright &copy; 2011-2020 lnint Inc. All right reserved.
 * 
 * 修改信息：【与SVN提交信息一致】
 * A: 新增类 王磊 2015-03-20 08:42:32
 */
package com.lnint.jess.generator.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lnint.jess.common.config.Global;
import com.lnint.jess.common.persistence.Page;
import com.lnint.jess.common.web.BaseController;
import com.lnint.jess.common.utils.StringUtils;
import com.lnint.jess.core.sys.entity.User;
import com.lnint.jess.core.sys.utils.UserUtils;
import com.lnint.jess.generator.entity.GenScene;
import com.lnint.jess.generator.service.GenSceneService;

/**
 * <P>代码生成场景管理Controller</P>
 *
 * @version 2015-03-20
 * @author 王磊 2015-03-20 08:42:32
 * @since JDK 1.6
 */
@Controller
@RequestMapping(value = "${adminPath}/generator/genScene")
public class GenSceneController extends BaseController {

	@Autowired
	private GenSceneService genSceneService;
	
	/**
	 * <P>ModelAttribute 构造函数</P>
	 * @param id 实体主键;
	 * @return GenScene
	 */
	@ModelAttribute
	public GenScene get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return genSceneService.get(id);
		}else{
			return new GenScene();
		}
	}
	/**
	 * <P>表单form查询函数</P>
	 * @param genScene 实体模型;
	 * @param request  请求;
	 * @param response 返回;
	 * @param model    视图模型;
	 * @return view URL
	 */
	@RequiresPermissions("generator:genScene:view")
	@RequestMapping(value = {"list", ""})
	public String list(GenScene genScene, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			genScene.setCreateBy(user);
		}
		//获得列表分页数据
        Page<GenScene> page = genSceneService.find(new Page<GenScene>(request, response), genScene); 
        model.addAttribute("page", page);
        //转向页面
		return "generator/genSceneList";
	}
	/**
	 * <P>表单form查询函数</P>
	 * @param genScene 实体模型;
	 * @param model 视图模型;
	 * @return view URL
	 */
	@RequiresPermissions("generator:genScene:view")
	@RequestMapping(value = "form")
	public String form(GenScene genScene, Model model) {
		//由ModelAttribute默认构造genScene实体模型
		model.addAttribute("genScene", genScene);
		//转向页面
		return "generator/genSceneForm";
	}
	
	/**
	 * <P>表单form修改函数</P>
	 * @param genScene 实体模型;
	 * @param model 视图模型;
	 * @return view URL
	 */
	@RequiresPermissions("generator:genScene:view")
	@RequestMapping(value = "editform")
	public String editform(GenScene genScene, Model model) {
		//由ModelAttribute默认构造genScene实体模型
		model.addAttribute("genScene", genScene);
		//转向页面
		return "generator/genSceneColumn";
	}

	/**
	 * <P>场景设置-下一步-转向 设置页面第二步 函数</P>
	 * @param genScene 实体模型;
	 * @param model 视图模型;
	 * @param redirectAttributes 重定向;
	 * @return view URL
	 */
	@RequiresPermissions("generator:genScene:edit")
	@RequestMapping(value = "goScenSeting")
	public String goScenSeting(GenScene genScene, Model model, RedirectAttributes redirectAttributes) {
		//实体Validator
		if (!beanValidator(model, genScene)){
			return form(genScene, model);
		}
		//根据场景选择，和主从表，获得物理表的字段信息给实体
		//1:获得表主键
//		String pk=genSceneService.findTablePK(genScene.getName());
		//2:获得表所有物理字段信息
		//主表类名
		genScene.setClassName(StringUtils.toCamelCase(genScene.getName()));
		if(genScene.getSceneType().equals("2")){//主从表场景
			//从表类名
			genScene.setSubclassName(StringUtils.toCamelCase(genScene.getSubname()));
			///2.1获得主表字段数据
			genScene.setColumnList(genSceneService.findTableColumnList(genScene.getName()));
			///2.2获得子从表字段数据
			genScene.setSubColumnList(genSceneService.findTableColumnList(genScene.getSubname()));
		}else{//主表or 可编辑列表 场景
			///2.3获得主表字段数据
			genScene.setColumnList(genSceneService.findTableColumnList(genScene.getName()));
		}

		//由ModelAttribute默认构造genScene实体模型
		model.addAttribute("genScene", genScene);
		
		//转向页面
		return "generator/genSceneColumn";
	}
	/**
	 * <P>表单form保存函数</P>
	 * @param genScene 实体模型;
	 * @param model 视图模型;
	 * @param redirectAttributes 重定向;
	 * @return view URL
	 */
	@RequiresPermissions("generator:genScene:edit")
	@RequestMapping(value = "save")
	public String save(GenScene genScene, Model model, RedirectAttributes redirectAttributes) {
		//实体Validator
		if (!beanValidator(model, genScene)){
			return form(genScene, model);
		}
		//保存表单
		genSceneService.save(genScene);
		//页面提示信息
		addMessage(redirectAttributes, "保存代码生成场景管理'" + genScene.getName() + "'成功");
		//转向页面
		return "redirect:"+Global.getAdminPath()+"/generator/genScene/?repage";
	}
	/**
	 * <P>列表list删除函数</P>
	 * @param id 实体主键;
	 * @param redirectAttributes 重定向;
	 * @return view URL
	 */	
	@RequiresPermissions("generator:genScene:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		//删除操作
		genSceneService.delete(id);
		//页面提示信息
		addMessage(redirectAttributes, "删除代码生成场景管理成功");
		//转向页面
		return "redirect:"+Global.getAdminPath()+"/generator/genScene/?repage";
	}

}
